/*
 * $Id$
 *
 * Copyright ( c ) 2014 XuNuo Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of XuNuo
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with XuNuo Corporation or a XuNuo
 * authorized reseller (the "License Agreement"). XuNuo may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, XuNuo DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. XuNuo DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * XuNuo SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF XuNuo HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. XuNuo' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE XuNuo SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.impl.service.internal.chargedpresc.command;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.vo.PdsSupplyDetailVO;
import com.th.supcom.pds.impl.service.internal.chargedpresc.AbstractCommand;
import com.th.supcom.pds.impl.service.internal.chargedpresc.ChargedPrescriptionContext;

/**
 * 药品出库
 * 
 * @author wangweiquan
 */
public class DrugStockOutCommand extends AbstractCommand implements Command
{

    @Override
    public boolean execute (Context context) throws Exception
    {
        ChargedPrescriptionContext chargedPrescriptionContext = (ChargedPrescriptionContext) context;
        Map <String, Object>map=chargedPrescriptionContext.getMap ();
        List <PdsSupplyDetailVO> pdsSupplyDetailVOList = chargedPrescriptionContext.getPdsSupplyDetails ();
        if (s_logger.isInfoEnabled ())
        {
            s_logger.info ("准备执行药品出库服务，参数信息：{}", context);
        }
        List <PdsSupplyDetailVO> list = new ArrayList <PdsSupplyDetailVO> ();
        String deptcode = (String) map.get ("dispensaryCode");
        for (PdsSupplyDetailVO pdsSupplyDetail : pdsSupplyDetailVOList)
        {
            if (deptcode.equals (pdsSupplyDetail.getPerformedBy()))
            {//在当前科室供药的药品出库
                list.add (pdsSupplyDetail);
            }
        }
        if (pdsSupplyDetailVOList.isEmpty ())
        {
            return false;
        }
        if (getDispenseDrugService ().needStockOutAtDispense (list))
        {
            s_logger.info ("药房配置在发药进行出库");
        }
        else
        {
            // 调用DMS接口进行药品出库并返回药品单据号
            this.getAdapterDrugManagerService ().drugStockOut (list, map);
            // 更新医嘱状态接口。传入参数：医嘱ID，处方号，医嘱状态；返回参数：无。
            this.getAdapterDrugManagerService ().sendStockOutInfo (list);
        }

        if (s_logger.isInfoEnabled ())
        {
            s_logger.info ("药品出库结束");
        }

        return false;
    }

    /** 日志记录器. */
    private static Logger s_logger = LoggerFactory.getLogger (DrugStockOutCommand.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
